package net.huashitong.supervision.gongjian.service.project;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.gongjian.dao.JungongTouchanDao;
import net.huashitong.supervision.gongjian.entity.project.JungongTouchanEntity;
import net.huashitong.supervision.gongjian.entity.project.ZhengQianEntity;
import net.huashitong.supervision.gongjian.filter.JungongTouchanFilter;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectFeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JungongTouchanService extends StringPKService<JungongTouchanEntity> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private JungongTouchanDao dao;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private ProjectFeedBackService feedBackService;

    @Override
    protected BaseDao<JungongTouchanEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(JungongTouchanEntity obj) {
        String sysId = tableService.updateMaxSysId("t_gj_jungongtouchan", null);
        obj.setSysId(sysId);
    }

    private JungongTouchanEntity setState(JungongTouchanEntity entity, int vaild , int pType){
        int feedbackCount = feedBackService.getFeedbackCount(entity.getSysId(), vaild, pType);
        if(1<=feedbackCount){
            entity.setCurrentBack(1);
        }else {
            entity.setCurrentBack(0);
        }
        return entity;
    }

    public List<JungongTouchanEntity> getList(JungongTouchanFilter filter)
    {
        List<JungongTouchanEntity> list = dao.getList(filter);
        for (JungongTouchanEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 4);
            setState(entity,1,4);
            entity.setZerenDept(projectDeptName);

        }
        return  list;
    }

    public List<JungongTouchanEntity> getListFowOwn(JungongTouchanFilter filter){
        filter.setProjectType(4);
        List<JungongTouchanEntity> list = dao.getListFowOwn(filter);
        for (JungongTouchanEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 4);
            setState(entity,1,4);
            entity.setZerenDept(projectDeptName);

        }

        return  list;
    }

    /**
     * 无分页
     * @param filter
     * @return
     */
    public List<JungongTouchanEntity> getListNoPage(JungongTouchanFilter filter)
    {
        List<JungongTouchanEntity> list = dao.getListNoPage(filter);

        return  list;
    }

}
